//
//  YDEmoticonToolBarView.swift
//  weibo
//
//  Created by 孙亚东 on 16/7/2.
//  Copyright © 2016年 Sunyadong. All rights reserved.
//

import UIKit


//设置代理

protocol YDEmoticonToolBarViewProtocol: NSObjectProtocol {


    func buttonClikDelegate (btn: UIButton)



}

enum barButtonType: Int {

    case Recent = 1000
    case Default = 1001
    case Emoji = 1002
    case Lxh = 1003

}

class YDEmoticonToolBarView: UIStackView {
    
    
    //定义一个代理
    
    var delegate: YDEmoticonToolBarViewProtocol?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
    }
    


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
        
    }
    
    func setupUI() {
    
    axis = .Horizontal
    distribution = .FillEqually

        addChildButton("Recently", imageName: "compose_emotion_table", type:  .Recent)
        addChildButton("Default", imageName: "compose_emotion_table", type:  .Default)
        addChildButton("Emoji", type:  .Emoji)
        addChildButton("LXH", type:  .Lxh)
    
    }

    // 创建button的方法
    func addChildButton (btnName: String, imageName: String = "compose_emotion_table" , type: barButtonType) {
    
        let button = UIButton()
        
        button.setTitle(btnName, forState: .Normal)
        
        button.setTitleColor(UIColor.grayColor(), forState: .Selected)
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)

        
        button.setBackgroundImage(UIImage(named: "\(imageName)_normal" ), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "\(imageName)_selected" ), forState: .Selected)
        
        button.addTarget(self, action: "buttonClick:", forControlEvents: .TouchUpInside)
        
        addArrangedSubview(button)
        
        button.tag = type.rawValue
        
        if type == .Default {
        
            button.selected = true
        
        }
        

    }
    
    
    func buttonClick (btn: UIButton) {
        
        
        for viewBtn in self.subviews {
        
            let tembtn = viewBtn as! UIButton
            
            tembtn.selected = false
            
        
        }
        
        
        btn.selected = true
     
        self.delegate?.buttonClikDelegate(btn)
    

    }
    
    
    
    

}
